package com.py.credit.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.py.credit.dao.DataReminderDao;
import com.py.credit.dao.DissentAppealDao;
import com.py.credit.entity.SysUser;
import com.py.credit.service.DataReminderService;
import com.py.credit.utils.UuidUtil;
import com.py.framework.core.common.Page;
@Service
public class DataReminderServiceImpl implements DataReminderService {
	
	@Autowired
	DataReminderDao dataReminderDao;
	
	@Override
	public Object run() {
		//获取所有异议处理数据
		List<Map<String,Object>> list = dataReminderDao.getAll();
		String decription="";
		String title="异议处理超时提醒";
		//根据异议处理数据的添加时间进行推送
		for(Map<String,Object> map :list) {
			if(Integer.parseInt(map.get("day").toString())>=3&&Integer.parseInt(map.get("day").toString())<5) {
				String batchId = UuidUtil.getInstance().getUUid();
				decription ="您有异议处理任务超过三天未处理，请及时处理";
				//先判断该数据有没有推送给部门管理员
				int count = dataReminderDao.selectexistence(Integer.parseInt(map.get("id").toString()),"部门管理员");
				if(count<=0) {
					//3-5天之内提醒所有部门管理员
					dataReminderDao.sendRoleUser(Integer.parseInt(map.get("id").toString()),"部门管理员",batchId,decription,title);
				}
				
				
			}else if(Integer.parseInt(map.get("day").toString())>5) {
				String batchId = UuidUtil.getInstance().getUUid();
				decription =map.get("sjly")+":有异议处理任务超过五个工作日未处理！";
				int count = dataReminderDao.selectexistence(Integer.parseInt(map.get("id").toString()),"系统管理员");
				if(count<=0) {
					//超过5天提醒所有系统管理员
					dataReminderDao.sendRoleUser(Integer.parseInt(map.get("id").toString()),"系统管理员",batchId,decription,title);
				}
				
			}
		}
		return null;
		
	}
	@Override
	public List<Map<String, Object>> getByRolenameForPage(Page page, SysUser userInfo,String msg_type) {
		
		return dataReminderDao.getByRolenameForPage(userInfo.getRoleName(),page,msg_type);
	}
	@Override
	public Object ReminderExceptionTask() {
			//获取所有异常处理数据
				List<Map<String,Object>> list = dataReminderDao.getExceptionAll();
				String decription="";
				String title="异常处理超时提醒";
				//根据异常处理数据的添加时间进行推送
				for(Map<String,Object> map :list) {
					if(Integer.parseInt(map.get("day").toString())>3) {
						String batchId = UuidUtil.getInstance().getUUid();
						decription ="您有异常处理任务超过三天未处理，请及时处理";
						//先判断该数据有没有推送给采集员
						int count = dataReminderDao.isException(map.get("batch_id").toString());
						if(count<=0) {
							//3-5天之内提醒采集员
							dataReminderDao.sendUser(map.get("batch_id").toString(),Integer.parseInt(map.get("manager_id").toString()),decription,title);
						}
				
						
					}
				}
				return null;
	}
	@Override
	public List<Map<String, Object>> getExceptionListForPage(Page page, SysUser userInfo,String msg_type) {
		return dataReminderDao.getExceptionListForPage(userInfo.getId(),page,msg_type);
	}
	@Override
	public void updateStatus(String tablename, String id) {
		dataReminderDao.updateStatus(tablename,id);
		
	}
	@Override
	public void datafeedbackTask() {
		//获取所有反馈数据
		List<Map<String,Object>> list = dataReminderDao.getdatafeedbackAll();
		
		String title="数据采集反馈";
		for(Map<String,Object> map :list) {
			String decription=String.valueOf(map.get("OFFICE_NAME"))+"反馈"+String.valueOf(map.get("resource_title"))+"反馈类型为:"+String.valueOf(map.get("type"))+"原因为："+String.valueOf(map.get("reason"));
		//先判断该数据有没有推送给采集员
		int count = dataReminderDao.isdatafeedback(map.get("batch_id").toString());
		if(count<=0) {
			dataReminderDao.addfeedback(map.get("batch_id").toString(),decription,title);
				}
		
		}
		
	}
	@Override
	public int getNewCount() {
		
		return dataReminderDao.getNewCount();
	}
	@Override
	public List<Map<String, Object>> getdatafeedbackForPage(Page page, SysUser userInfo,String msg_type) {
		// TODO Auto-generated method stub
		return dataReminderDao.getdatafeedbackForPage(msg_type,page);
	}
	@Override
	public int getfeedbackCount() {
		// TODO Auto-generated method stub
		return dataReminderDao.getfeedbackCount();
	}
	@Override
	public int getexceptionCount(Long id) {
		// TODO Auto-generated method stub
		return dataReminderDao.getexceptionCount(id);
	}
	@Override
	public int getdisapprealCount(String roleName) {
		// TODO Auto-generated method stub
		return dataReminderDao.getdisapprealCount(roleName);
	}
	
	@Override
	public void deletedata(String id, String tablename) {
		dataReminderDao.deletedata(id,tablename);
	}
	
	
	@Override
	public void XkztUpdateTask() {
		dataReminderDao.XkyxUpdateZrrTask();
		dataReminderDao.XkwxUpdateZrrTask();
		dataReminderDao.XkyxtUpdateFrTask();
		dataReminderDao.XkwxUpdateFrTask();
	}

}
